package agency.sales;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

import agency.Sales;
import dict.AgencyDict;
import dict.BcDict;
import entities.TBank;
import entities.TBc;
import entities.TCard;
import entities.TTransaction;
import net.sf.json.JSONObject;
import util.HttpUtil;
import util.LogUtil;
import utils.AesUtil;
import utils.DateUtil;
import utils.DigestUtil;
import utils.NumberUtil;
import utils.StringUtil;

public class Etpdcc implements Sales {

	private static String PATH = "https://ncp.etonpay.com/Gateway";
	private static String RETU = "https://gateway.etonpay.com/Receive";
	private String CURRENCY = "CNY";
	private String PRODUCT = "PANTS";

	public void process(TBc bc, TTransaction transaction, TBank bank, TCard card) {
		Map<String, String> requestMap = new TreeMap<String, String>();
		JSONObject responseMap = new JSONObject(); 
		String res = null;
		String[] params = null;
		// 消费

		requestMap.put("mer", bank.getBnkMer());
		requestMap.put("website", transaction.getTxnSite());
		requestMap.put("order", bc.getBcNo());
		requestMap.put("loc", NumberUtil.transfer(bc.getBcAmount()));
		requestMap.put("cur", CURRENCY);
		requestMap.put("signure", DigestUtil.sha256(bank.getBnkMer() + bc.getBcNo() + bank.getBnkKey()));
		requestMap.put("return", RETU);
		requestMap.put("notify", RETU);
		requestMap.put("product", PRODUCT);
		requestMap.put("phone", card.getCrdCphone());
		requestMap.put("cname", card.getCrdCfname() + " " + card.getCrdClname());
		requestMap.put("cemail", card.getCrdCemail());
		requestMap.put("ccountry", card.getCrdCcountry());
		requestMap.put("cstate", card.getCrdCstate());
		requestMap.put("ccity", card.getCrdCcity());
		requestMap.put("caddr", card.getCrdCaddr());
		requestMap.put("czip", card.getCrdCzip());
		requestMap.put("hname", card.getCrdHfname() + " " + card.getCrdHlname());
		requestMap.put("hemail", card.getCrdHemail());
		requestMap.put("hcountry", card.getCrdHcountry());
		requestMap.put("hstate", card.getCrdHstate());
		requestMap.put("hcity", card.getCrdHcity());
		requestMap.put("haddr", card.getCrdHaddr());
		requestMap.put("hzip", card.getCrdHzip());
		requestMap.put("ip", card.getCrdIp());
		requestMap.put("os", card.getCrdOs());
		requestMap.put("browser", card.getCrdBrowser());

		try {
			String expire=AesUtil.AES_Decrypt(AesUtil.KEY, card.getCrdExpire());
			String cvv=AesUtil.AES_Decrypt(AesUtil.KEY, card.getCrdCvv());
			requestMap.put("card", AesUtil.AES_Encrypt(DigestUtil.md5(bank.getBnkKey()).toUpperCase(), card.getCrdNumber()));
			requestMap.put("expire", AesUtil.AES_Encrypt(DigestUtil.md5(bank.getBnkKey()).toUpperCase(), expire.substring(2,4)+expire.substring(0,2)));
			requestMap.put("cvv", AesUtil.AES_Encrypt(DigestUtil.md5(bank.getBnkKey()).toUpperCase(), cvv));
		} catch (Exception e) {
			bc.setBcMark("555");
			bc.setBcInfo("edc sale data encrpt exception");
			bc.setBcCode(AgencyDict.ETP_ENCRYPT_EXCEPTION);
			bc.setBcMessage(AgencyDict.AGENCY.get(AgencyDict.ETP_ENCRYPT_EXCEPTION));
			return;
		}

		// 日志打印发送数据
		StringBuffer send = new StringBuffer();
		for (Map.Entry<String, String> e : requestMap.entrySet()) {
			send.append(e.getKey() + "=" + e.getValue() + "&");
			// System.out.println(e.getKey()+"==="+e.getValue());
		}
		LogUtil.agency("ETPEDC_SALES", send.toString());

		try {
			res = new HttpUtil().ssl(PATH, requestMap);
		} catch (Exception e) {
			bc.setBcMark("555");
			bc.setBcInfo("edc sale data encrpt exception");
			bc.setBcCode(AgencyDict.ETP_TRANSMISSION_EXCEPTION);
			bc.setBcMessage(AgencyDict.AGENCY.get(AgencyDict.ETP_TRANSMISSION_EXCEPTION));
			return;
		}

		if (StringUtil.isEmpty(res)) {
			bc.setBcMark("555");
			bc.setBcInfo("edc sale data response exception");
			bc.setBcCode(AgencyDict.ETP_TRANSMISSION_EXCEPTION);
			bc.setBcMessage(AgencyDict.AGENCY.get(AgencyDict.ETP_TRANSMISSION_EXCEPTION));
			return;
		}

		try {
			responseMap = JSONObject.fromObject(res); 
		} catch (Exception e) {
			bc.setBcMark("555");
			bc.setBcInfo("edc sale data disembly exception");
			bc.setBcCode(AgencyDict.ETP_TRANSMISSION_EXCEPTION);
			bc.setBcMessage(AgencyDict.AGENCY.get(AgencyDict.ETP_TRANSMISSION_EXCEPTION));
			return;
		}

		// 日志打印返回数据
		LogUtil.agency("ETPEDC_SALES", res);

		if (!responseMap.containsKey("statusCode")) {
			bc.setBcMark("555");
			bc.setBcInfo("edc sale data parameter exception");
			bc.setBcCode(AgencyDict.ETP_TRANSMISSION_EXCEPTION);
			bc.setBcMessage(AgencyDict.AGENCY.get(AgencyDict.ETP_TRANSMISSION_EXCEPTION));
			return;
		}

		if (!responseMap.get("statusCode").equals("success")) {
			bc.setBcMark(responseMap.get("errorCode").toString());
			bc.setBcInfo(responseMap.get("errorCode").toString());
			bc.setBcCode(AgencyDict.ETP_SALES_FAILURE);
			bc.setBcMessage(AgencyDict.AGENCY.get(AgencyDict.ETP_SALES_FAILURE));
			return;
		}

		// 如果成功
		Map<String,String> resultMap = new HashMap<String,String>();
		params = responseMap.get("errorCode").toString().split("&");
		for (int i = 0; i < params.length; i++) {
			String[] p = params[i].split("=");
			if (p.length == 2) {
				resultMap.put(p[0], p[1]);
			}
		}

		bc.setBcState("1");
		bc.setBcRef(resultMap.get("refund").toString());
		bc.setBcCard(card.getCrdFake());
		bc.setBcTime(DateUtil.format(new Date(), DateUtil.DATE));
		bc.setBcMark(responseMap.get("statusCode").toString());
		bc.setBcInfo(responseMap.get("statusCode").toString());
		bc.setBcCode(BcDict.BC_SUCCESS);
		bc.setBcMessage(BcDict.MESSAGES.get(BcDict.BC_SUCCESS));
		return;
	}

}